﻿#******************************************************************************************************************************
# SPPackager.SPDeploy.DeployWebApplicationFarmSolution.ps1 - Perform the instalation, upgrade and retraction of Ms Sharepoint Applications fast, easy and efficient
# Script version 1.0 - Jan, 30th of 2013
# Frederico Souza Lima (fredericosl@hotmail.com)
# Michael Capela (michaelcapela@hotmail.com)
# Copyright (c) 2012-2013 
#******************************************************************************************************************************

param([string]$FarmSolutionWSPName, [string]$WebApplicationURL, [bool]$GACDeployment)

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function WaitForJobToFinish([string]$SolutionFileName)
{ 
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) 
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
        
        while ((Get-SPTimerJob $JobFullName) -ne $null) 
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job.."
    }
}

$SPSolution = Get-SPSolution | Where-Object {$_.Name -eq $FarmSolutionWSPName};

$SPWebApplication = Get-SPWebApplication $WebApplicationURL;

if ($SPSolution.ContainsWebApplicationResource) {
	Install-SPSolution –Identity $SPSolution -Confirm:$false -GACDeployment:$GACDeployment -WebApplication $SPWebApplication -Force:$true;
}
else
{
	Install-SPSolution –Identity $SPSolution -Confirm:$false -GACDeployment:$true -Force:$true;
}
Write-Host 'Waiting for job to finish';
WaitForJobToFinish $FarmSolutionWSPName;
